
// Anulima Choudhary 
// Roll No. 2010018

#include<stdio.h>
#include<stdlib.h>

int *arr,n;

// add element num to end of list
void append(int *p,int num)
{  p=realloc(p,(n+1)*sizeof(int));
   p[n]=num;
   n=n+1;
}

// display the list
void display(int *p)
{   int i;
    printf("\n the array is=>");    
    for (i=0; i<n; i++) 
    {
          printf("%d\t",p[i]);
    }
    
}

// add a list at the end of the original list
int extend(int b[100],int size)
{
	int i;
	arr=realloc(arr,(n+size)*sizeof(int*));
	for(i=0;i<size;i++)
	{ 
		
		arr[n]=b[i];
		n++;
	}
	return n;
}

// reverses the list
void reverse(int *p)
{ int i;
  
  for(i=n-1;i>=0;i--)
  {
          printf("%d\t",p[i]);
   }
}

// deletes the ith element of the list and returns its value
//assuming array starts from 1
int pop(int i)
{       int value,j;
	value=arr[i];
	for(j=i-1;j<n;j++)
	{
		arr[j-1]=arr[j];
		
	}
	n--;
	return value;
	
}

// sort the list
//comparison function is passed as parameter
void sort(int ch)
{
int i,j,temp;

for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{       if(ch==1) // increasing
			{ if(arr[j]>arr[i])
			    {
				temp=arr[i];
				arr[i]=arr[j];
				arr[j]=temp;
			     }
		         }
                        else if(ch==0)
                        { if(arr[j]<arr[i])
                            { 
                               temp=arr[i];
                               arr[i]=arr[j];
                               arr[j]=temp;
                             }
                         }
	}
}
}

//returns the number of occurences of num in list
int count(int num)
{
	int c=0,i;
        int flag=0;
		for(i=0;i<=n;i++)
		{
			flag=1;
			if(arr[i]==num)
			{
				c++;
			}
		}
	if(flag==0)
	printf("number not found!!");
		
return c;
}

// deletes the first occurence of num in list
void remove1(int num)
{ int i,j,flag=0;
  for(i=0;i<n;i++)
  { if(arr[i]==num)
      {   flag=1;
          for(j=i;j<n;j++)
          arr[j]=arr[j+1];
	  n--;
      }
    if(flag==0)
       {printf("element not found!!");}
	

  }
} 

// insert num into list at index loc
void insert(int loc,int num)
{ int j;
  n=n+1;
  for(j=loc;j<=n;j++)
  { arr[j+1]=arr[j];
   }
   arr[loc]=num;
}

// returns index of first occurence of x
int index1(int x)
{ int c=0,i;
  for(i=0;i<n;i++)
  {  if(arr[i]==x)
      { return i;
      }
  }
}
        

void main()
{ int ch;
  arr=(int*)malloc(n*sizeof(int));
  do
  {  
  
    printf("\n 1.append an element in the list->");
    printf("\n 2.display the list-> ");
    printf("\n 3.insert at a particular location->");
    printf("\n 4.reverse->");
    printf("\n 5. remove->");
    printf("\n 6. pop->");
    printf("\n 7. extend the list->");
    printf("\n 8. sort->");
    printf("\n 9.count->");
    printf("\n 10. index->");
    printf("\n 11. exit!!");
    printf("\n enter your choice:");
    scanf("%d",&ch);

    switch(ch)
    {
         
     case 1:     {  int e;
                    printf("\n enter the element to append :");
                    scanf("%d",&e);
                    append(arr,e);
                    break;
                  }

     case 2:      {  printf("\n the elements in the list are:");
                     display(arr);
                     break;
    
     case 3:      { int i,num;
                    printf("enter the element:");
                    scanf("%d",&num);
                    printf("enter the location:");
                    scanf("%d",&i);
                    insert(i,num);
                    break;
                   }
               }
     case 4:       {printf("the reversed array is=>");
                    reverse(arr);
                    break;}
     case 5:      { int s;
                    printf("enter element to be removed->");
                    scanf("%d",&s);
                    remove1(s);
                    break;
                  }
     case 6:       {
			int i,e;
			printf("Enter the position at which you want to delete or 0 if you don't want to specify the position :");
			scanf(" %d",&i);
			if (i==0)
			e=pop(n-1);
			else
			e=pop(i-1);
			printf("Element deleted is %d",e);
			break;
                    } 
     case 7:
		  {     int i;
			int b[100],k;
			printf("enter the number of elements in array: ");
			scanf("%d",&k);
                        printf(" enter the elements:\n");
			for (i=0;i<k;i++)
			{       printf("element %d=",i+1);
				scanf("%d",&b[i]);
			}
			extend(b,k);
			break;
		}
      case 8:
		{       int ch;
                        printf(" enter 1 for sorting the list in increasing order and 0 for sorting in decreasing order:");
                        scanf("%d",&ch);
			sort (ch);
			break;
		}
     case 9:    {
			int x,num;
			printf("Enter the number to be counted ");
			scanf(" %d",&num);
			x=count(num);
			printf("number of times %d appears is=%d ",num,x);
			break;
	 	}
    case 10:
		{
			int num,m;
			printf("Enter the number whose index you want to know: ");
			scanf("%d",&num);
			m=index1(num);
                        printf("index of %d is=%d",num,m);
			break;
		}
     case 11:      exit(0);
     default:     printf(" invalid output!");
  }
 }while((ch!=12));
}

//****************************END******************************8

